#include "xconfig.h"
#include <fstream>
#include <iostream>

using namespace std;

bool XConfig::Read(const std::string &filename)
{
    fstream ifs(filename, ios::in);

    if (!ifs.is_open())
    {
        return false;
    }

    string line;

    while (1)
    {
        getline(ifs, line);
        if (!line.empty())
        {
            auto pos = line.find('=');
            if (pos <= 0)
                continue;
            if (pos == string::npos)
                continue;

            auto key = line.substr(0, pos);
            auto value = line.substr(pos + 1);
            conf_.insert({key, value});
        }

        if (!ifs.good())
        {
            break;
        }
    }
    return true;
}

const std::string XConfig::Get(const std::string &key) const
{
    auto p = conf_.find(key);
    if (p == conf_.end())
    {
        return "";
    }
    return p->second;
}
